// 配置marked.js
marked.setOptions({
    highlight: function(code, lang) {
        if (lang && hljs.getLanguage(lang)) {
            return hljs.highlight(code, { language: lang }).value;
        }
        return hljs.highlightAuto(code).value;
    },
    breaks: true,
    gfm: true
});

// Markdown渲染函数
function renderMarkdown(element) {
    const content = element.textContent;
    element.innerHTML = marked.parse(content);
    
    // 为所有代码块添加复制按钮
    element.querySelectorAll('pre code').forEach(codeBlock => {
        const pre = codeBlock.parentNode;
        if (!pre.classList.contains('code-block-wrapper')) {
            const wrapper = document.createElement('div');
            wrapper.className = 'code-block-wrapper relative';
            
            const copyButton = document.createElement('button');
            copyButton.className = 'copy-button absolute top-2 right-2 bg-gray-700 text-gray-300 text-xs px-2 py-1 rounded opacity-0 transition-opacity';
            copyButton.innerHTML = '<i class="fas fa-copy mr-1"></i>复制';
            
            copyButton.addEventListener('click', function() {
                navigator.clipboard.writeText(codeBlock.textContent).then(() => {
                    copyButton.innerHTML = '<i class="fas fa-check mr-1"></i>已复制';
                    setTimeout(() => {
                        copyButton.innerHTML = '<i class="fas fa-copy mr-1"></i>复制';
                    }, 2000);
                });
            });
            
            pre.parentNode.replaceChild(wrapper, pre);
            wrapper.appendChild(pre);
            wrapper.appendChild(copyButton);
            
            wrapper.addEventListener('mouseenter', () => {
                copyButton.classList.remove('opacity-0');
            });
            
            wrapper.addEventListener('mouseleave', () => {
                copyButton.classList.add('opacity-0');
            });
        }
    });
}